/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Product;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author OkSa Bawu
 */
public class ProductDAOImpl implements ProductDAO {

    private EntityManager em;

    public ProductDAOImpl(EntityManager em) {
        this.em = em;
    }

    @Override
    public void insert(Product p) throws Exception {
        try {
            em.getTransaction().begin();
            em.persist(p);
            em.getTransaction().commit();
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(Product p) throws Exception {
        try {
            em.getTransaction().begin();
            em.remove(p);
            em.getTransaction().commit();
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(Product p) throws Exception {
        try{
            em.getTransaction().begin();
            em.merge(p);
            em.getTransaction().commit();
        }catch(Exception ex){
            throw ex;
        }
    }

    @Override
    public Product get(Long id) throws Exception {
        Product p = null;
        try {
            em.getTransaction().begin();
            p = em.find(Product.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            throw ex;
        }
        return p;
    }

    @Override
    public List<Product> gets() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Product> getByCode(String code) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
